/**
 * Create by cheese znnozomi@163.com
 * 2018/5/5
 */


/*对象解构*/
let node = {
    loc: {
        start: {
            line: 1,
            column: 5
        }
    }
};

let {
    loc,
    loc: { start },
    loc: { start: { line } }
} = node;

console.log(line);
console.log(loc);
console.log(start);

let obj = {};
let arr = [];

({ foo: obj.prop, bar: arr[0] } = { foo: 123, bar: true });

console.log(obj);
console.log(arr);

let _out = '我是全局变量';

{
    let _in = '我是局部变量';
    console.log(_out);
}

{
    // console.log(_in);  //not defined
}

{

    let t = f();

    console.log(t * t + 1);

    function f() {

        return 2;
    };
}

console.log(f());


{
    console.log(''.charAt(0))
}

/*
内建函数
 */
{
    let s = new String('hello world');
    console.log(s);
    console.log(typeof s);
    console.log(s instanceof String);
    console.log(s.toString());
    console.log(Object.prototype.toString.call(s));

    let obj = new Object();
    console.log(obj);
    console.log(Object.prototype);

    /*let _number = new Number(2);
    console.log(_number.toFixed());
    console.log(new Number());*/
}

/*

********************************
 */


let firstObj = {};

console.log({});

let secondObj = { a: 1, b: 2, c: 3 };
firstObj = secondObj;

//  firstObj.d = 4;

console.log(firstObj);
console.log(secondObj);

{
    let obj = new Object();

    console.log('--proto---');
    console.log(obj);
    console.log(obj.hasOwnProperty('name'));


    function Person(name, age) {
        this.name = name;
        this.age = age;
    }

    console.log(Person.prototype);
    Person.prototype.MaxNumber = 9999;
    Person.__proto__.MinNumber = -9999;
    var will = new Person("Will", 28);
    console.log(will.MaxNumber); // 9999
    console.log(will.MinNumber); // undefined
}


/*
* 数组去重
* */
{
    var __arr = [0, 2, 3, 4, 4, 0, 2];
    var __obj = {};
    var tmp = [];
    for (var i = 0; i < __arr.length; i++) {
        if (!__obj[__arr[i]]) {
            __obj[__arr[i]] = 1;
            tmp.push(__arr[i]);
        }

        // console.log(__obj);
    }
    console.log(tmp);
}

{
    function fun(n, o) {
        console.log(o);
        return {
            fun: function (m) {
                return fun(m, n);
            }
        }
    }
    var a = fun(0); a.fun(1); a.fun(2); a.fun(3);
    var b = fun(0).fun(1).fun(2).fun(3);
    var c = fun(0).fun(1); c.fun(2); c.fun(3);

    console.log(a);
    console.log(b);
    console.log(c);

    var _a = 9;

    /* console.log(_a++);
     console.log(_a--);
     console.log(++_a);
     console.log(--_a);
     console.log(_a--);*/


    var _b = _a++ + _a-- + ++_a + --_a + _a--;
    console.log(_a);//8
    console.log(_b);//47

}

{
    var objName = "name1";
    function obj() {
        var objName = "name2";
        function innerObj() {
            // alert(objName);//name2
        }
        innerObj();
    }
    console.log(obj());//undefined
}



//一个更高效的  可以 不用嵌套循环 比对前后选择勾选的放法
{
    // 缓存数据
    var students = [
        { id: 35, name: '小明', age: 25, address: '环球中心', checked: true },
        { id: 36, name: '杰伦', age: 41, address: '中国台湾', checked: true },
        { id: 37, name: '不撸死', age: 46, address: '霉国', checked: true }
    ];

    // 最新数据
    var data = [
        { id: 35, name: '小明', age: 25, address: '环球中心', checked: false },
        { id: 36, name: '杰伦', age: 41, address: '中国台湾', checked: false },
        { id: 37, name: '不撸死', age: 46, address: '霉国', checked: false },
        { id: 38, name: '大明', age: 46, address: '哈哈哈哈哈', checked: false },
        { id: 39, name: '中明', age: 46, address: '中国四川', checked: false }
    ];

    function Array2Json(arr, obj = {}) {
        // arr.forEach(item => {
        //     obj[item.id] = item;
        // })

        arr.map((val, index) => {
            obj[val.id] = val;
        });

        return obj
    }

    students = Array2Json(students);

    console.log(students);

    let i = 0;
    while (i < data.length) {

        if (students[data[i].id]) {

            data[i].checked = true;
        }

        i++;
    }

    console.log(data);
    // console.log(students);
}


{


    let obj_1 = {

        a: '我是obj_1的a',

        log: function () {
            console.log(this.a);
        }
    }

    obj_1.log();


    let obj_2 = {
        a: '我是obj_2的a'
    }

    obj_1.log.call(obj_2);

    console.log(Object.prototype.toString.call([1, 2, 3, 4]));

    console.log(typeof [1, 2, 3, 4, 4]);

    console.log([1, 2, 3, 4].toString());

}

//冒泡排序
{
    let arr = [2, 5, 9, 4, 3, 88];

    for (let i = 0; i < arr.length - 1; i++) {

        for (let j = 0; j < arr.length - i - 1; j++) {

            if (arr[j] > arr[j + 1]) {

                let swap = arr[j];

                arr[j] = arr[j + 1];
                arr[j + 1] = swap;

            }
        }
    }

    console.log(arr);
}

//二分查找
{
    function binarySearch(arr, dest) {


        let h = arr.length - 1,
            l = 0;

        while (l <= h) {
            let m = Math.floor((h + 1) / 2);
            if (arr[m] == dest) {
                return m;
            }

            if (dest > arr[m]) {
                l = m + 1;
            } else {
                h = m - 1;
            }
        }

        return false;
    }

    let data = [-24, 2, 4, 5, 62, 54, 543, 35, 4, 7];

    // console.log(binarySearch(data,543));
}

//二分查找 递归写法
{

    let binaryFind = (data, dest, start = data.length - 1, end = 0) => {


        m = Math.floor((start + end) / 2);

        if (data[m] === dest) {
            return m;
        }

        if (dest > data[m]) {

            return binaryFind(data, dest, m + 1, end);
        } else {

            return binaryFind(data, dest, 0, m - 1);
        }
    }

    let arr = [-24, 1, 3, 4, 5, 8, 34, 45, 65, 87];

    console.log(binaryFind(arr, 4, ));

}







